package com.yupi.web.manager;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @version 1.0
 * @Author moresuo
 * @Date 2024/9/27 18:37
 * @注释
 */
@Component
public class CacheManager {
    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    Cache<String,Object> localCache= Caffeine.newBuilder()
            .expireAfterWrite(100, TimeUnit.MINUTES)
            .maximumSize(10_1000)
            .build();

    public void put(String key,Object value){

        localCache.put(key, value);
        redisTemplate.opsForValue().set(key,value,100,TimeUnit.MINUTES);
    }

    public Object get(String key){
        Object value = localCache.getIfPresent(key);
        if(value!=null){
            return value;
        }
        value = redisTemplate.opsForValue().get(key);
        if(value!=null){
            localCache.put(key,value);
        }
        return value;
    }
    public void delete(String key){
        localCache.invalidate(key);
        redisTemplate.delete(key);
    }
}
